﻿using UnityEngine;
using System.Collections;
using ProtoBuf.Message;

namespace UI
{
    public class UICopyResetChanllangeCount : Page
    {
        public UILabel popupBoxTitle;
        public UILabel popupBoxContent;
        string str = "";
        string title = "";
        int needCostDiamond = 20;
        string resetLevelId = "";
        string resetChapterId = "";
        void Awake()
        {
            str = TableTextBlo.GetText("reset_level_content");
            popupBoxContent.text = string.Format(str, needCostDiamond);
            title = TableTextBlo.GetText("reset_level");
            popupBoxTitle.text = title;
        }

        protected override void DoOpen()
        {
            if(options.ContainsKey("costRuby"))
            {
                var costRuby = options["costRuby"];

                var intCostRuby = int.Parse(costRuby);
                needCostDiamond = intCostRuby; 
            }
            else
            {

            }

            resetLevelId = options["resetLevelId"];
            resetChapterId = options["resetChapterId"];

        }

        protected override void DoClose()
        {
        }


        #region event
        public void OnClickOk()
        {
            if (StaticData.playerData.diamond >= needCostDiamond)
            {
                CSCopyResetChallengeCount reset = new CSCopyResetChallengeCount();
                reset.chapterId = resetChapterId;
                reset.missionId = resetLevelId;
                reset.cost = new BaseResource();
                reset.cost.num = needCostDiamond;
                reset.cost.id = "diamond";

                NetAPI.SendHttp(OpDefine.CSCopyResetChallengeCount, reset, (obj, ok) => 
                {
                    if (ok)
                    {
                        StaticData.playerData.SubDiamond(needCostDiamond);
                        UICopyData.ResetCurrentLevelChallengeCount(resetLevelId);

                        Close();
                    }
                });
            }
            else
            {
                Debug.Log("[DEBUG] need open recharge page");
            }
        }

        public void OnClickCancel()
        {
            Close();
        }
        #endregion

    }
}